﻿#include <iostream>
#include <direct.h>
#include <string.h>
#include <fstream>
#include <string>
#include <vector>
#include<algorithm>

using namespace std;

string PathProcessing(char* path)
{
    string ret(path);
    string::size_type pos = 0;
    while ((pos = ret.find('\\', pos)) != string::npos)
    {
        ret.insert(pos, "\\");//插入  
        pos = pos + 2;
    }
    return ret;
}

int main(int argc, char* argv[])
{
    //获取工程路径
    cout << "###########################################\n";
    cout << "STM32 VScode Env configuration V0.2\n";
    char path[_MAX_PATH];
    _getcwd(path, _MAX_PATH);
    std::string projectPath = PathProcessing(path);
    std::cout << "Stm Project Path = " << projectPath.c_str() << "\n";
    //获取配置文件路径
    char* str = 0;
    _get_pgmptr(&str);
    strrchr(str, '\\')[0] = '\0';
    std::string filesPtah = PathProcessing(str);
    std::cout << "Config Files Path = " << filesPtah.c_str() << "\n";
    //复制配置文件
    string scriptPath = filesPtah + "\\ConfigFile\\UserScript\\*";
    string targetScriptPath = projectPath + "\\UserScript";
    string copyScriptCMD = "xcopy " + scriptPath + " " + targetScriptPath + " /s/y/i";
    if (system(copyScriptCMD.c_str()) == -1)
    {
        std::cout << "Config Files Copy Error!!!! \n";
        return 0;
    }
    //复制.vscode文件
    string vsPath = filesPtah + "\\ConfigFile\\.vscode\\*";
    string targetVsPath = projectPath + "\\.vscode";
    string copyVsCMD = "xcopy " + vsPath + " " + targetVsPath + " /s/y/i";
    if (system(copyVsCMD.c_str()) == -1)
    {
        std::cout << "VS Files Copy Error!!!! \n";
        return 0;
    }
    //查找当前是否存在makefile
    fstream makefile;
    string makeFilePath = projectPath + "\\Makefile";
    makefile.open(makeFilePath.c_str(), ios::in);
    if (!makefile)
    {
        cout << "Makefile Open Error !!!! \n";
        cout << "Please at STM32CubeMX generate Makefile project！！！！\n";
        makefile.close();
        return 0;
    }
    //读取makefile
    //vector<string> makeFileContent;
    vector<string> makeFileDefine;
    vector<string> makeFileInclude;
    string makeTarget;
    string fileLine;
    while (getline(makefile, fileLine)) // line中不包括每行的换行符
    {
        fileLine.erase(std::remove(fileLine.begin(), fileLine.end(), '\\'), fileLine.end());
        //replace(fileLine.begin(), fileLine.end(), '\\', ' ');
        if (fileLine.find("TARGET = ") != -1)
        {
            makeTarget = fileLine.substr(9);
        }
        else if (fileLine.find("-D") != -1)
        {
            makeFileDefine.push_back(fileLine.substr(2));
        }
        else if (fileLine.find("-I") != -1)
        {
            fileLine.erase(fileLine.find_last_not_of(" ") + 1);
            makeFileInclude.push_back(fileLine.substr(2));
        }
    }
    makefile.close();
    //修改调试文件中调试名
    fstream launchJson;
    string launchJsonPath = projectPath + "\\.vscode\\launch.json";
    launchJson.open(launchJsonPath.c_str(), ios::binary | ios::out | ios::in);
    if (!launchJson)
    {
        cout << "launch.Json Open Error !!!! \n";
        return 0;
    }
    string launchJsonfileLine;
    string launchJsonData;
    while (getline(launchJson, launchJsonfileLine)) // line中不包括每行的换行符
    {  
        if (launchJsonfileLine.find("x.elf") != -1)
        {
            launchJsonfileLine.erase(launchJsonfileLine.find("x.elf"),1);
            launchJsonfileLine.insert(launchJsonfileLine.find(".elf"), makeTarget);
        }
        launchJsonData = launchJsonData + launchJsonfileLine;
        //launchJsonData += "\n";
    }
    launchJson.close();
    ofstream launchOutJson(launchJsonPath.c_str());
    launchOutJson << launchJsonData;
    launchOutJson.close();
    cout << "launch.Json Edit OK !!!! \n";
    //添加宏定义以及头文件路径
    fstream cppJson;
    string cppJsonPath = projectPath + "\\.vscode\\c_cpp_properties.json";
    cppJson.open(cppJsonPath.c_str(), ios::binary | ios::out | ios::in);
    if (!cppJson)
    {
        cout << "c_cpp_properties.Json Open Error !!!! \n";
        return 0;
    }
    string cppJsonfileLine;
    string cppJsonData;
    while (getline(cppJson, cppJsonfileLine)) // line中不包括每行的换行符
    {
        cppJsonData = cppJsonData + cppJsonfileLine;
        if (cppJsonfileLine.find("\"${workspaceFolder}/**\"") != -1)
        {

            cppJsonData += ",\n";
            for (auto ii = makeFileInclude.begin(); ii != makeFileInclude.end(); ++ii)
            {
                string wStr = "\t\t\t\t\"${workspaceFolder}/" + *ii + "\"";
                if (ii + 1 != makeFileInclude.end())
                {
                    wStr += ",\n";
                }
                cppJsonData += wStr;
            }
        }
        if (cppJsonfileLine.find("\"_UNICODE\"") != -1)
        {

            cppJsonData += ",\n";
            for (auto ii = makeFileDefine.begin(); ii != makeFileDefine.end(); ++ii)
            {
                string wStr = "\t\t\t\t\"" + *ii + "\"";
                if (ii + 1 != makeFileDefine.end())
                {
                    wStr += ",\n";
                }
                cppJsonData += wStr;
            }
        }
        cppJsonData += "\n";
    }
    cppJson.close();
    ofstream cppOutJson(cppJsonPath.c_str());
    cppOutJson << cppJsonData;
    cppOutJson.close();
    cout << "c_cpp_properties.Json Edit OK !!!! \n";
    cout << "###########################################\n";
    return 0;
}
